package com.bluebench.bigul.domain.graphics.simple.polygon;

import java.util.ArrayList;
import java.util.List;

import com.bluebench.bigul.domain.graphics.Graphic;
import com.bluebench.bigul.domain.graphics.Line;
import com.bluebench.bigul.domain.graphics.Point;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.PdfContentByte;

public class Trapezium extends Quadilateral{

	public Trapezium(PdfContentByte canvas) {
		super(canvas);		
	}

	@Override
	public Graphic build() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	Polygon build(Point start, List<Float> sides) {
		// TODO Auto-generated method stub
		return null;
	}

	public Trapezium build(Point start, List<Float> sides, double angle){
		
		float x0 = start.getX();
		float y0 = start.getY();
		float horz = sides.get(0);
		float vert = sides.get(1);
				
		angle = convertToRadian(angle);		
		
		float x1 = x0 + horz;
		float y1 = y0;
        
        float x2 = (float)(x0 + horz - (vert * Math.cos(angle)));
        float y2 = (float)(y0 + (vert * Math.sin(angle)));
        
        float x3 = (float)(x0+(vert * Math.cos(angle)));
        float y3 = (float)(y0 + (vert * Math.sin(angle)));		
        
        vertices.add(new Point(x0,y0));
        vertices.add(new Point(x1,y1));
        vertices.add(new Point(x2,y2));
        vertices.add(new Point(x3,y3));        
        
        if(validate()){
	        canvas.moveTo(x0,y0); 
	        canvas.lineTo(x1, y1);
	        canvas.lineTo(x2,y2);
	        canvas.lineTo(x3,y3);
        }      
        
        return this;		
	}
	
public Trapezium build(Point start, List<Float> sides, double angle, float rotation){
		
		float x0 = start.getX();
		float y0 = start.getY();
		float horz = sides.get(0);
		float vert = sides.get(1);
				
		angle = convertToRadian(angle);		
		
		float x1 = x0 + horz;
		float y1 = y0;
        		
        float x2 = (float)(x0 + horz - (vert * Math.cos(angle)));
        float y2 = (float)(y0 + (vert * Math.sin(angle)));
        
        float x3 = (float)(x0+(vert * Math.cos(angle)));
        float y3 = (float)(y0 + (vert * Math.sin(angle)));		
        
        Point p0 = new Point(x0,y0);
        Point p1 = new Point(x1,y1);
        Point p2 = new Point(x2,y2);
        Point p3 = new Point(x3,y3);
        
        vertices.add(p0);
        vertices.add(p1);
        vertices.add(p2);
        vertices.add(p3);        
        
        Line base = new Line(p0,p1);
        Line right = new Line(p1,p2);
        Line top = new Line(p2,p3);
        Line left = new Line(p3,p0);
        
        Point pivot = getPivot(base,right,top,left); 
        
        Point p0New = getRotation(p0, pivot, rotation);       
        Point p1New = getRotation(p1, pivot, rotation);      
        Point p2New = getRotation(p2, pivot, rotation);        
        Point p3New = getRotation(p3, pivot, rotation);
       
        
        if(validate()){
	        canvas.moveTo(p0New.getX(),p0New.getY()); 
	        canvas.lineTo(p1New.getX(),p1New.getY());
	        canvas.lineTo(p2New.getX(),p2New.getY());
	        canvas.lineTo(p3New.getX(),p3New.getY());
        }      
        
        return this;		
	}

	

	/**
	 * Opposite sides must be parallel
	 */
	@Override
	protected boolean validate() {
		
		float x0 = vertices.get(0).getX();
		float y0 = vertices.get(0).getY();
		
		float x1 = vertices.get(1).getX();
		float y1 = vertices.get(1).getY();

		float x2 = vertices.get(2).getX();
		float y2 = vertices.get(2).getY();

		float x3 = vertices.get(3).getX();
		float y3 = vertices.get(3).getY();
		
		float tan1 = (y1-y0)/(x1-x0);
		float tan2 = (y3-y2)/(x3-x2);
		
		if(tan1==tan2){
			return true;
		}
		
		return false;
	}

	@Override
	protected Graphic concentric(float distance, boolean in) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Graphic copy(float distance, int direction) {
		// TODO Auto-generated method stub
		return null;
	}

}
